'use strict';

// https://help.aliyun.com/document_detail/32068.html

const Controller = require('egg').Controller;
const fs = require('mz/fs');
const Client = require('@alicloud/ocr-2019-12-30');

// 创建实例
const client = new Client({
  accessKeyId: 'LTAIOL60W1RI80wB',
  accessKeySecret: 'IKH9qiA0Cl9Pg2YpjhMeIEAdWqltHi',
  securityToken: '', // 支持STS
  endpoint: 'https://ocr.cn-shanghai.aliyuncs.com'
});

class AliOcrController extends Controller {
  async index() {
    const { ctx } = this;
    // 发起调用
    // 具体API，返回Promise
    // 车牌识别

    let imgUrl = 'https://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/RecognizeLicensePlate/licensePlate.jpg';
    let data = await MyrecognizeLicensePlate('https://ocrhgdq.oss-cn-shanghai.aliyuncs.com/jinN8p8f8.jpg');
    ctx.body = {
      requestBody: data
    };
  }
}

function MyrecognizeLicensePlate(imgUrl){
  return new Promise((resolve,reject) => {
    const data = client.recognizeLicensePlate({
      "ImageURL": imgUrl
    }, {timeout: 10000}).then(function (res) {
      resolve(res);
    }, function (err) {
      console.log('err = ',err);
      resolve(err);
    });
  })
}

module.exports = AliOcrController;
